<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/4/1
 * Time: 下午1:49
 */

namespace App\Http\Controllers\Auth;


use App\Http\Controllers\BaseController;
use App\Logic\MemberLogic;
use App\Logic\PublicLogic;
use Illuminate\Http\Request;
use Validator;

/**
 * 登录、注册等公共接口
 *
 * Class PublicController
 * @package App\Http\Controllers\Auth
 */
class PublicController extends BaseController
{

    public function __construct()
    {
        $this->setToken(false);
        parent::__construct();
    }

    /**
     * 注册
     *
     * @param Request $request
     * @return array
     */
    public function register(Request $request)
    {
        $rule = [
            'mobile' => 'required|regex:/^1[34578][0-9]{9}$/',
            'password' => 'required|string',
            'code' => 'required|string',
        ];
        $message = [
            'mobile.regex' => '手机号码有误',
            'password.required' => '密码不能为空',
            'code.required' => '验证码不能为空',
        ];
        $validator = Validator::make($request->all(), $rule, $message);

        $errors = $validator->errors()->all(); //这是捕获到的所有参数验证的错误信息

        //如果验证不通过
        if (!empty($errors) && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        return MemberLogic::register($request->all());
    }

    /**
     * 登录
     * @param Request $request
     * @return array|void
     */
    public function login(Request $request)
    {
        $rule = [
            'mobile' => 'required|regex:/^1[34578][0-9]{9}$/',
            'password' => 'required|string',
        ];
        $message = [
            'mobile.regex' => '手机号码有误',
            'password.required' => '密码不能为空',
        ];
        $validator = Validator::make($request->all(), $rule, $message);

        $errors = $validator->errors()->all();

        //如果验证不通过
        if (!empty($errors) && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        return MemberLogic::login($request->all());
    }

    /**
     * 忘记密码
     *
     * @param Request $request
     * @return array|void
     */
    public function forget(Request $request)
    {
        $rule = [
            'mobile' => 'required|regex:/^1[34578][0-9]{9}$/',
            'password' => 'required|string',
            'code' => 'required|string',
        ];
        $message = [
            'mobile.regex' => '手机号码有误',
            'password.required' => '密码不能为空',
            'code.required' => '验证码不能为空',
        ];
        $validator = Validator::make($request->all(), $rule, $message);

        $errors = $validator->errors()->all(); //这是捕获到的所有参数验证的错误信息

        //如果验证不通过
        if (!empty($errors) && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        return MemberLogic::forget($request->all());
    }

    /**
     * 轮播图
     *
     * @return array
     */
    public function banner()
    {
        return PublicLogic::banner(2);
    }


}